//
//  Color.m
//  Bespoke
//
//  Created by Cheng on 12/8/2.
//
//

#import "Color.h"
#import "AccountColor.h"
#import <QuartzCore/QuartzCore.h>

#define COLOR_CIRCLE_SIZE 20
#define LEFT_BOUNDARY 17
#define RIGHT_BOUNDARY 17
#define BORDER_WIDTH 10
#define TOP_BOUNDARY 25
#define BORDER_HEIGHT 10
#define BOTTOM_SPACE 10

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

@implementation Color
@synthesize Array_Color, IVw_Background;
@synthesize delegate;

- (int)OptionArray:(NSMutableArray *)Initialization
{
    int X_axis = LEFT_BOUNDARY;
    int Y_axis = TOP_BOUNDARY;
    int i=0;
    self.Array_Color = [NSMutableArray array];
    for(AccountColor *color in Initialization) {
        
        UIButton *colorLabelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        colorLabelButton.frame = CGRectMake(X_axis, Y_axis, COLOR_CIRCLE_SIZE, COLOR_CIRCLE_SIZE);
        
        [self.Array_Color addObject:color];
        colorLabelButton.tag = [self.Array_Color indexOfObject:color];
        
        colorLabelButton.titleLabel.adjustsFontSizeToFitWidth = TRUE;
        
        [colorLabelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        int HexValue;
        sscanf([color.Color_HexValue cStringUsingEncoding:NSASCIIStringEncoding], "%x", &HexValue);
        
        colorLabelButton.layer.cornerRadius = 10.0f;
        colorLabelButton.layer.masksToBounds = NO;
        colorLabelButton.layer.borderColor = [UIColor grayColor].CGColor;
        
        [colorLabelButton setBackgroundColor:UIColorFromRGB(HexValue)];
        [colorLabelButton setTag:i++];
        [colorLabelButton addTarget:self action:@selector(colorbuttonPress:)  forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:colorLabelButton];
        
        if (X_axis+RIGHT_BOUNDARY<self.frame.size.width-colorLabelButton.frame.size.width)
        {
            X_axis = X_axis + BORDER_WIDTH + colorLabelButton.frame.size.width;
            
            //Check Last Object.
            if ([Initialization indexOfObject:color]==Initialization.count-1)
                 Y_axis = Y_axis + BORDER_HEIGHT + colorLabelButton.frame.size.height;
        }
        else {
            X_axis = LEFT_BOUNDARY;
            Y_axis = Y_axis + BORDER_HEIGHT + colorLabelButton.frame.size.height;
        }
    }
    
    if (Y_axis+BOTTOM_SPACE>180)
        IVw_Background.image = [UIImage imageNamed:@"Color_dropdown_6lines.png"];
    else if (Y_axis+BOTTOM_SPACE>100)
        IVw_Background.image = [UIImage imageNamed:@"Color_dropdown_4lines.png"];
    else
        IVw_Background.image = [UIImage imageNamed:@"Color_dropdown_2lines.png"];
    
    return Y_axis+BOTTOM_SPACE;
}

-(void)colorbuttonPress:(id)sender
{
    AccountColor *LoadColor = [self.Array_Color objectAtIndex:[sender tag]];
    [delegate uiViewTouched_Color:NO ];
    [delegate DidSelected_Color:LoadColor.Color_Name];
}

#pragma mark - Touches
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    if( point.x > 0 && point.x < self.frame.size.width && point.y > 0 && point.y < self.frame.size.height )
    {
        [delegate uiViewTouched_Color:YES ];
        return YES;
    }
    
    [delegate uiViewTouched_Color:NO ];
    return NO;
}

@end
